package com.vhall.component.service.interactive.impl;

import com.alibaba.fastjson.JSON;
import com.vhall.component.entity.export.ExportEntity;
import com.vhall.component.entity.room.dto.RoomsEntityDTO;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.constant.ConstStr;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.service.export.ExportService;
import com.vhall.component.service.interactive.QaConsoleService;
import com.vhall.component.service.room.RoomInnerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Administrator
 */
@Service
@Slf4j
public class QaConsoleServiceImpl implements QaConsoleService {

    @Autowired
    private RoomInnerService roomInnerService;
    @Autowired
    private ExportService exportService;

    @Override
    public Boolean create(String fileName, String appId, String roomId, Integer accountId, String startTime, String endTime) {
        RoomsEntityDTO liveInfo = roomInnerService.getRoomInfo(roomId);
        if (liveInfo == null) {
            throw new BusinessException(BizErrorCode.EMPTY_ROOM);
        }
        String beginTime = startTime + " " + ConstStr.TIME_ZERO;
        String overTime = endTime + " " + ConstStr.TIME_FULL;

        ExportEntity ee = new ExportEntity();
        ee.setExport("qa");
        ee.setIlId(liveInfo.getIlId());
        ee.setAccountId(accountId);
        ee.setSourceId("");
        ee.setFileName(fileName);
        List<String> titles = new ArrayList<>();
        titles.add("类型");
        titles.add("账号");
        titles.add("昵称");
        titles.add("角色");
        titles.add("问答内容");
        titles.add("私密");
        titles.add("状态");
        titles.add("同问人数");
        titles.add("更新时间");
        ee.setTitle(JSON.toJSONString(titles));
        Map<String, Object> params = new HashMap<>(8);
        params.put("app_id", appId);
        params.put("room_id", liveInfo.getRoomId());
        params.put("start_time", beginTime);
        params.put("end_time", overTime);
        ee.setParams(JSON.toJSONString(params));
        ee.setExt("csv");
        ee.setCallback("qaService:getQaExportData");
        ee.setRoomId(roomId);
        return exportService.createExport(ee);

    }
}
